﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;
using dhDataAcess.Model;
using dhDataAcess.linqDAL;

namespace dhDataAcess.sysDAL
{
   public  class basListDAL
    {
       string tableName = "basList";
        private SqlTransaction tran = null;
        private SqlConnection conn = null;
        public basListDAL(SqlTransaction _transaction, SqlConnection _conn)
        {
            tran = _transaction;
            conn=_conn;
        }

      
        /// <summary>
        /// 插入一行基础信息表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>

        public int basListInsert(basListModel deptRow)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string insertStr = baseBLL.dTableColumnRead.insertSqlStr(tableName).ToString();
            //string insertStr = "insert into basList (mark,createDate,rmark,isSys,basNameII,basNameI,basNum,basType) values(@mark,@createDate,@rmark,@isSys,@basNameII,@basNameI,@basNum,@basType)";
            SqlParameter[] para = {
                          new SqlParameter ("@mark",deptRow.mark  ),
                          new SqlParameter ("@createDate",deptRow .createDate ),
                          new SqlParameter ("@rmark",deptRow .rmark ),
                          new SqlParameter ("@isSys",deptRow .isSys ),
                          new SqlParameter ("@basNameII",deptRow .basNameII),
                          new SqlParameter ("@basNameI",deptRow .basNameI),
                          new SqlParameter ("@basNum",deptRow .basNum ),
                          new SqlParameter ("@basType",deptRow .basType)
};

            cmd.Parameters.AddRange(para);
            cmd.CommandText = insertStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }

        /// <summary>
        /// 更新一行基础信息表数据 
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>

        public int basListUpdate(basListModel deptRow)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string updateStr = baseBLL.dTableColumnRead.updateSqlStr(tableName).ToString();
            //string updateStr = "update basList set mark=@mark,createDate=@createDate,rmark=@rmark,isSys=@isSys,basNameII=@basNameII,basNameI=@basNameI,basType=@basType where basNum=@basNum";
            SqlParameter[] para = {
                                   new SqlParameter ("@recordid ",deptRow.recordid  ),
                                   new SqlParameter ("@mark",deptRow.mark  ),
                                   new SqlParameter ("@createDate",deptRow .createDate ),
                                   new SqlParameter ("@rmark",deptRow .rmark ),
                                   new SqlParameter ("@isSys",deptRow .isSys ),
                                   new SqlParameter ("@basNameII",deptRow .basNameII),
                                   new SqlParameter ("@basNameI",deptRow .basNameI),
                                   new SqlParameter ("@basNum",deptRow .basNum ),
                                   new SqlParameter ("@basType",deptRow .basType)
                                  };
           
            cmd.Parameters.AddRange(para);
            cmd.CommandText = updateStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }


        /// <summary>
        /// 删除一行基础信息表数据
        /// </summary>
        /// <param name="basNum"></param>
        /// <returns></returns>

        public int basListDel(string recordid)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string deleteStr = baseBLL.dTableColumnRead.delSqlStr (tableName).ToString();
           // string deleteStr = "delete basList where basNum=@basNum";
            SqlParameter[] para = {
                                new SqlParameter ("@recordid ",recordid )
                              };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = deleteStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 根据项目编号返回项目名称
        /// </summary>
        /// <param name="basNum"></param>
        /// <returns></returns>

        public string basListretbasNameI(string basNum)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string selectStr = "selete basNameI from basList where basNum=@basNum";
            SqlParameter[] para = {
                                new SqlParameter ("@basNum",basNum),
                              };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = selectStr;
            cmd.Transaction = tran;
            SqlDataReader rdr = cmd.ExecuteReader();
            string basNameI = null;
            while (rdr.Read())
            {
                basNameI = rdr[0].ToString();
            }

            return basNameI;
        }

       //更新订单号
        public void basListretbasNumUpdate(string basNum,string basType)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string selectStr = "update basList set basNum=@basNum where basType=@basType";
            SqlParameter[] para = {
                                new SqlParameter ("@basNum",basNum),
                                new SqlParameter ("@basType",basType),
                              };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = selectStr;
            cmd.Transaction = tran;
            cmd.ExecuteNonQuery();
        }

        //public SqlDataReader basListretbasNameTest(List<queryModel> queryList)
        //{
        //    SqlCommand cmd = new SqlCommand();
        //    cmd.Connection = conn;
        //    cmd.CommandType = CommandType.Text;
        //    string selectStr = "select * from basList where ";

        //    int i = 0;
        //    foreach (queryModel query in queryList)
        //    {
        //        if (i == 0)
        //        {
        //            selectStr = selectStr + query.strField + query.strBJ + "@" + query.strFieldParm;
        //        }
        //        else
        //        {
        //            selectStr = selectStr +" or "+ query.strField + query.strBJ + "@" + query.strFieldParm;
        //        }
        //        i++;
        //    }



        //    foreach (queryModel query in queryList)
        //    {
        //        SqlParameter parm = new SqlParameter("@"+query.strFieldParm, query.strValue);
        //        cmd.Parameters.Add(parm);
        //    }

        //    cmd.CommandText = selectStr;
        //    cmd.Transaction = tran;
        //    SqlDataReader rdr = cmd.ExecuteReader();
        //    SqlDataAdapter da = new SqlDataAdapter();
        

        //    return rdr;
        //}

    }
}
